package com.hengpeng.common.util;

import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Properties;

import javax.mail.MessagingException;
import javax.mail.internet.AddressException;

import com.hengpeng.common.constant.Constant;
import com.hengpeng.common.mail.Contact;
import com.hengpeng.common.mail.Sender;

/**
 * <pre>
 * TODO 邮件发送
 * </pre>
 * @author pengfangliang
 * @version 1.0, 2010-9-28
 */
public class MailSendUtil
{
	/**
	 * <pre>
	 *  邮件单发
	 * </pre>
	 * @param sendto 接收地址
	 * @param subject 邮件主题
	 * @param html 邮件类容
	 * @throws AddressException
	 * @throws UnsupportedEncodingException
	 * @throws MessagingException
	 */
	public static void sendMail(String sendto, String subject, String html) throws AddressException,
			UnsupportedEncodingException, MessagingException
	{
		Properties props = getProperties();
		Sender sender = new Sender(props);
		sender.sendHtmlBase64(sendto, subject, html);
	}

	/**
	 * <pre>
	 *  邮件群发
	 * </pre>
	 * @param contacts 联系人集合
	 * @param subject 邮件主题
	 * @param html 邮件内容
	 * @throws UnsupportedEncodingException
	 * @throws MessagingException
	 */
	public static void sendMailList(List<Contact> contacts, String subject, String html)
			throws UnsupportedEncodingException, MessagingException
	{
		Properties props = getProperties();
		Sender sender = new Sender(props);
		sender.sendMailList(contacts, subject, html);
	}

	/**
	 * <pre>
	 *  邮件发送配置
	 * </pre>
	 * @return
	 */
	private static Properties getProperties()
	{
		Properties props = new Properties();
		props.put("mail.smtp.host", Constant.MAIL_SERVER);
		props.put("mail.smtp.auth", Constant.MAIL_AUTH);
		props.put("mail.from", Constant.MAIL_FROM);
		props.put("mail.from.password", Constant.MAIL_FROM_PASSWORD);
		return props;
	}

	public static void main(String args[]) throws AddressException, UnsupportedEncodingException, MessagingException
	{
		String to = "191010607@qq.com";
		String subject = "邮件测试主题";
		StringBuffer content = new StringBuffer();
		content.append("邮件测试");
		MailSendUtil.sendMail(to, subject, content.toString());
		System.out.println("Send mail OK. " + to);
	}
}
